/*
 * SteepestDescentSlover.cpp
 *
 *  Created on: 2015-1-4
 *      Author: cpp
 */

#include "SteepestDescentSlover.h"
#define MAX_ITER 1000000
#define tol 1.0e-20
Vec * SteepestDescentSlover::slove(Matrix& A,Vec & b){
	Vec* x = b.construct(A.getColL());
	Vec* r = b.clone();
	int k = 0;
	double norm = r->norm(2);
	double bnorm = b.norm(2);
	while(norm>tol*bnorm){
		k++;
		Vec* tmp = A*(*r);
		double a = ((*r)*(*r))/((*r)*(*tmp));
		delete tmp;
		for(int i=0;i<x->len();i++){
			x->set(i,x->get(i)+a*r->get(i));
		}
		delete r;
		r = A*(*x);
		(*r)*=(-1.0);
		(*r)+=b;
		norm = r->norm(2);
	}
	delete r;
	return x;
}
